﻿/*
	This file is part of Limpet.

    Limpet is free software: you can redistribute it and/or modify
    it under the terms of the GNU Lesser General Public License as published by
    the Free Software Foundation, either version 3 of the License, or
    (at your option) any later version.

    Limpet is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU Lesser General Public License for more details.

    You should have received a copy of the GNU Lesser General Public License
    along with Limpet.  If not, see http://www.gnu.org/licenses/.
	
	Copyright 2010 Dan Popick
*/
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Text;

namespace Open.Web.Diagnostics.Limpet.Modules
{
    public sealed class LimpetTraceListener: TraceListener
    {
        public int MaxEntries { get; private set; }

        public LimpetTraceListener()
        {
        }

        public override void TraceEvent(TraceEventCache eventCache, string source, TraceEventType eventType, int id)
        {
            TraceEvent(eventCache, source, eventType, id, String.Empty);
        }

        public override void TraceEvent(TraceEventCache eventCache, string source, TraceEventType eventType, int id, string format, params object[] args)
        {
            if (Filter != null && !Filter.ShouldTrace(eventCache, source, eventType, id, format, args, null, null))
                return;

            SessionTraceQueue.AddEntry(new SessionTraceQueue.TraceEvent()
            {
                Type = eventType,
                Message = String.Format(format, args)
            });
        }

        public override void TraceEvent(TraceEventCache eventCache, string source, TraceEventType eventType, int id, string message)
        {
            if (Filter != null && !Filter.ShouldTrace(eventCache, source, eventType, id, message, null, null, null))
                return;

            SessionTraceQueue.AddEntry(new SessionTraceQueue.TraceEvent()
            {
                Type = eventType,
                Message = message == null ? String.Empty : message
            });
        }

        public override void TraceData(TraceEventCache eventCache, string source, TraceEventType eventType, int id, object data)
        {
            if (Filter != null && !Filter.ShouldTrace(eventCache, source, eventType, id, null, null, data, null))
                return;

            SessionTraceQueue.AddEntry(new SessionTraceQueue.TraceEvent()
            {
                Type = eventType,
                Message = data == null ? String.Empty : data.ToString()
            });
        }

        public override void TraceData(TraceEventCache eventCache, string source, TraceEventType eventType, int id, params object[] data)
        {
            if (Filter != null && !Filter.ShouldTrace(eventCache, source, eventType, id, null, null, null, data))
                return;

            StringBuilder builder = new StringBuilder();
            if (data != null)
            {
                bool comma = false;
                foreach (object o in data) 
                {
                    if (comma)
                        builder.Append(", ");
                    comma = true;

                    if (o != null)
                        builder.Append(o.ToString());
                }

                SessionTraceQueue.AddEntry(new SessionTraceQueue.TraceEvent()
                {
                    Type = eventType,
                    Message = builder.ToString()
                });
            }
        }

        public override void Write(string message)
        {
            SessionTraceQueue.AddEntry(new SessionTraceQueue.TraceEvent()
            {
                Type = TraceEventType.Information,
                Message = message
            });
        }

        public override void WriteLine(string message)
        {
            SessionTraceQueue.AddEntry(new SessionTraceQueue.TraceEvent()
            {
                Type = TraceEventType.Information,
                Message = message
            });
        }
    }
}
